package cn.bdqn.hospital.service.impl;

import cn.bdqn.hospital.dao.EmployeesDao;
import cn.bdqn.hospital.dao.impl.EmployeesDaoImpl;
import cn.bdqn.hospital.entity.Employees;
import cn.bdqn.hospital.service.EmployeesService;
import cn.bdqn.hospital.utils.DruidUtils;

import java.sql.Connection;

public class EmployeesServiceImpl implements EmployeesService {
    private EmployeesDao employeesDao = new EmployeesDaoImpl();

    /**
     * 新增员工
     * @param employees
     * @return
     * @throws Exception
     */
    @Override
    public boolean add(Employees employees) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            num = employeesDao.add(employees);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection);
        }
        return num == 1;
    }

    /**
     * 修改员工
     * @param employees
     * @return
     * @throws Exception
     */
    @Override
    public boolean update(Employees employees) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            num = employeesDao.update(employees);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return num == 1;
    }

    /**
     * 通过id删除员工
     * @param ygid
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteByYgid(Integer ygid) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            num = employeesDao.deleteByYgid(ygid);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return num == 1;
    }

    /**
     * 通过员工姓名删除员工
     * @param ygName
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByYgName(String ygName) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            num = employeesDao.deleteByYgName(ygName);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return 0;
    }

    /**
     * 通过员工账号删除员工
     * @param ygAccount
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByYgAccount(String ygAccount) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            num = employeesDao.deleteByYgAccount(ygAccount);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return 0;
    }

    /**
     * 通过员工id查询员工
     * @param ygid
     * @return
     * @throws Exception
     */
    @Override
    public Employees findEmployeesById(Integer ygid) throws Exception {
        Employees employees = null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            employees = employeesDao.findEmployeesById(ygid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return employees;
    }

    /**
     * 通过员工账号查询员工
     * @param ygAccount
     * @return
     * @throws Exception
     */
    @Override
    public Employees findEmployeesByAccount(String ygAccount) throws Exception {
        Employees employees = null;
        Connection connection = null;
        try{
            connection = DruidUtils.getConnection();
            employees = employeesDao.findEmployeesByAccount(ygAccount);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return employees;
    }

    /**
     * 通过员工手机号查询员工
     * @param ygPhone
     * @return
     * @throws Exception
     */
    @Override
    public Employees findEmployeesByPhone(String ygPhone) throws Exception {
        Employees employees = null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            employees = employeesDao.findEmployeesByPhone(ygPhone);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return employees;
    }
}
